<script setup lang="ts" name="Goods">
import useStore from '@/store'
import { storeToRefs } from 'pinia'
import { ref, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsInfo from './components/goods-info.vue'
import GoodsSale from './components/goods-sale.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/message'
import { CartItem } from '@/types/cart'
const store = useStore()
const { goods, cart } = store
const route = useRoute()

const { info } = storeToRefs(goods)

// 监听路由的变化发请求
watchEffect(() => {
  const goodsId = route.params.id
  if (route.fullPath === `/goods/${goodsId}`) {
    // 清空 Pinia 里面的旧数据
    // goods.clearGoodsInfo()
    goods.getGoodsInfo(goodsId as string)
  }
})

/* const goodsId = route.params.id
if (route.fullPath === `/goods/${goodsId}`) {
  goods.getGoodsInfo(goodsId as string)
} */

const currentSkuId = ref('')

const changeSku = (skuId: string) => {
  // #1 装一下选中的 skuId，为了后续做加入购物车的操作
  currentSkuId.value = skuId
  const sku = info.value.skus.find((sku) => sku.id === skuId)
  if (sku) {
    info.value.price = sku.price
    info.value.oldPrice = sku.oldPrice
    info.value.inventory = sku.inventory
  }
}
const count = ref(1)

// #3 加入购物车函数
const addCart = async () => {
  if (!currentSkuId.value) return Message.error('请选中规格')
  // 根据选中的 skuId 去所有 skus 里面找对应的那一个具体的 sku
  const sku = info.value.skus.find((item) => item.id === currentSkuId.value)!
  // 加工这个 sku.specs 加工为一个字符串，后续需要用到这个信息
  const attrsText = sku.specs.map((item) => item.name + ':' + item.valueName).join(' ')
  await cart.addCart({
    skuId: currentSkuId.value, // 选中的 skuId
    count: count.value, // 数量
    id: info.value.id, // 商品的 id
    name: info.value.name, // 名字
    picture: info.value.mainPictures[0], // 图片
    price: info.value.price, // 价钱
    attrsText, // 规格名和选中的具体规格的结合
    selected: true, // 选中状态
    nowPrice: info.value.price, // 价钱
    stock: info.value.inventory, // 库存
    isEffective: true, // 有效性
  } as CartItem)
  /* await cart.addCart({
    skuId: currentSkuId.value,
    count: count.value,
  })
  Message.success('加入购物车成功') */
  Message.success('加入购物车成功')
}
</script>

<template>
  <div class="xtx-goods-page">
    <!-- !这个判断一定不能少 -->
    <div class="container" v-if="info.categories">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${info.categories[1].id}`">{{ info.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${info.categories[0].id}`">{{ info.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem>{{ info.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左边 -->
        <div class="media">
          <!-- 图片 -->
          <GoodsImage :images="info.mainPictures" />
          <!-- 描述 -->
          <GoodsSale :goods="info" />
        </div>
        <!-- 右边 -->
        <div class="spec">
          <!-- 详情 -->
          <GoodsInfo :goods="info" />
          <!-- 规格组件 -->
          <GoodsSku :goods="info" @changeSku="changeSku" />
          <!-- 数量加减 -->
          <!-- count: {{ count }} -->
          <XtxNumbox v-model="count" />

          <XtxButton style="margin-top: 20px" @click="addCart">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail :goods="info" />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot type="1" />
          <GoodsHot type="2" />
          <GoodsHot type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
